/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import POJO.LichChieuPOJO;
import com.mysql.jdbc.CallableStatement;
import com.mysql.jdbc.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author kimloan
 */
public class LichChieuDAO {
    
    public boolean themLichChieu(LichChieuPOJO lc) {
        boolean kq = false;
        String sql = String.format("INSERT INTO lichchieu(idPhong,idPhim,NgayChieu,GioChieu)"
                + " VALUES(%d,%d,'%s','%s')", lc.getMaPhong(), lc.getMaPhim(), lc.getNgayChieu(),lc.getGioChieu());
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        int n = helper.executeUpdate(sql);
        if (n == 1) {
            kq = true;//Them LichChieu thanh cong
        }

        helper.close();
        return kq;
    }

    /**
     * Phuong Thuc Xoa LichChieu
     * @return
     */
    public boolean xoaLichChieu(String maLichChieu) {

        boolean kq = false;
        try {
            String sql = "delete from lichchieu where idLichChieu=" + maLichChieu;
            // String sql = "DELETE FROM LichChieu WHERE maLichChieu ='"+  maLichChieu+"'";
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            int n = helper.executeUpdate(sql);
            if (n >= 1) {
                kq = true;//xoa thanh cong
            }
            helper.close();
            return kq;
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            return kq;
        }
    }

    /**
     * Phuong Thuc Cap Nhat Lich Chieu
     */
    public boolean capNhatLichChieu(LichChieuPOJO lc) {
        boolean kq = false;

        String sql = String.format(
                "UPDATE lichchieu SET idPhong=%d,idPhim=%d,NgayChieu='%s',GioChieu='%s'"
                + " WHERE idLichChieu=%d", lc.getMaPhong(), lc.getMaPhim(), lc.getNgayChieu(),lc.getGioChieu(),lc.getMaLichChieu());
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        int n = helper.executeUpdate(sql);
        if (n == 1) {
            kq = true;//Cap Nhat Lich Chieu thanh cong
        }
        helper.close();
        return kq;
    }

    /**
     * Phuong Thuc Xem Danh LichChieu
     * @return
     */
    public ArrayList<LichChieuPOJO> layDanhLichChieuCacMaLichChieu() {
        ArrayList<LichChieuPOJO> ds = new ArrayList<LichChieuPOJO>();
        try {
            String sql = "SELECT * FROM lichchieu";
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            ResultSet rs = helper.executeQuery(sql);
            while (rs.next()) {
                LichChieuPOJO lc = new LichChieuPOJO();
                lc.setMaLichChieu(rs.getInt("idLichChieu"));
                lc.setMaPhong(rs.getInt("idPhong"));
                lc.setNgayChieu((java.util.Date)rs.getDate("NgayChieu"));
                lc.setGioChieu((java.util.Date)rs.getDate("GioChieu"));
                ds.add(lc);
            }
            helper.close();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return ds;
    }

    /**
     * Phuong Thuc Lay Thong Tin LichChieu
     * @param maLichChieu
     * @return
     */
    public LichChieuPOJO layThongTinLichChieu(String maLichChieu) {
        LichChieuPOJO LichChieu = null;
        String sql = String.format("SELECT * FROM lichchieu WHERE idLichChieu=%d", maLichChieu);
        try {
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            ResultSet rs = helper.executeQuery(sql);
            if (rs.next()) {
                LichChieu = new LichChieuPOJO();
                LichChieu.setMaLichChieu(rs.getInt("idLichChieu"));
                LichChieu.setMaPhong(rs.getInt("idPhong"));
                LichChieu.setNgayChieu((java.util.Date)rs.getDate("NgayChieu"));
                LichChieu.setGioChieu((java.util.Date)rs.getDate("GioChieu"));

            }
            helper.close();
        } catch (SQLException ex) {

            System.out.println(ex.getMessage());
        }
        return LichChieu;
    }
    //Ham Lay danh LichChieu theo the loai

    public ArrayList<LichChieuPOJO> layDSLichChieu_TheoTheLoai(int maTheLoai) {

        ArrayList<LichChieuPOJO> dsLichChieu = new ArrayList<LichChieuPOJO>();
        try {
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            Connection cn = (Connection) helper.getConnection();
            CallableStatement statement = (CallableStatement) cn.prepareCall("{call sp_LayDSLichChieu_TheoTheLoai(?)}");
            statement.setInt(1, maTheLoai);

            statement.execute();
            ResultSet rs = statement.getResultSet();
            while (rs.next()) {
                LichChieuPOJO LichChieu = new LichChieuPOJO();
                LichChieu.setMaLichChieu(rs.getInt("idLichChieu"));
                LichChieu.setMaPhong(rs.getInt("idPhong"));
                LichChieu.setNgayChieu((java.util.Date)rs.getDate("NgayChieu"));
                LichChieu.setGioChieu((java.util.Date)rs.getDate("GioChieu"));
                dsLichChieu.add(LichChieu);

            }
            helper.close();
            return dsLichChieu;
        } catch (Exception ex) {
            System.out.print(ex.getMessage());
            return dsLichChieu;
        }
    }

    public ArrayList<LichChieuPOJO> timKiemLichChieu_TheoTenLichChieu(String _tenLichChieu) {

        ArrayList<LichChieuPOJO> ds = new ArrayList<LichChieuPOJO>();
        try {
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            Connection cn = (Connection) helper.getConnection();
            CallableStatement statement = (CallableStatement) cn.prepareCall("{call sp_LayLichChieuTheoTenLichChieu(?)}");
            statement.setString(1, _tenLichChieu);
            statement.execute();
            ResultSet rs = statement.getResultSet();
            while (rs.next()) {
                LichChieuPOJO LichChieu = new LichChieuPOJO();
                LichChieu.setMaLichChieu(rs.getInt("idLichChieu"));
                LichChieu.setMaPhong(rs.getInt("idPhong"));
                LichChieu.setNgayChieu((java.util.Date)rs.getDate("NgayChieu"));
                LichChieu.setGioChieu((java.util.Date)rs.getDate("GioChieu"));
                ds.add(LichChieu);
            }
            helper.close();
        } catch (Exception ex) {
            System.out.print(ex.getMessage());
        }
        return ds;
    }
}
